// pages/print/photoList/photoList.js
const utils = require("../../../utils/utils");
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        list: [],
        chooseImageArray: [],
        code: '',
        switch1 : false
    },
    onChange(event){
        var arr = this.data.color.split(',')
        if(arr.length<2){
            utils.toast('当前打印机不能切换')
            return false
        }
        var index = event.currentTarget.dataset.index;
        const detail = event.detail.value;
        var color = 1;
        if(detail){
            color=2
        }else{
            color=1
        }
        this.setData({
            ['list['+index+'].color']:color
        })
        
    },
    minus(e) {
        var index = e.currentTarget.dataset.index;
        var num = e.currentTarget.dataset.num;
        var list = this.data.list;
        if (num == 1) {
            utils.toast('打印张数最少为1');
            return false;
        } else {
            list[index].num--;
            this.setData({
                list: list
            })
        }
    },
    add(e) {
        var index = e.currentTarget.dataset.index;
        var list = this.data.list;
        list[index].num++;
        this.setData({
            list: list
        })
    },
    clickImg(e) {
        var index = e.currentTarget.dataset.index;
        var img = this.data.list[index].printUrl;
        var str = img.split('?');
        if (img.indexOf('rotate,90') != '-1') {
            img = str[0] + '?x-oss-process=image/resize,h_2400,m_lfit/rotate,90/auto-orient,0';
        } else {
            img = str[0] + '?x-oss-process=image/auto-orient,0';
        }
        wx.navigateTo({
            url: '/pages/print/pictureEdit/pictureEdit?img=' + escape(img) + '&index=' + index,
        })
    },
    deleImg(e) {
        var that = this;
        var index = e.currentTarget.dataset.index;
        var array = that.data.list;
        wx.showModal({
            title: '提示',
            content: '确认删除这张图片吗？',
            success(res) {
                if (res.confirm) {
                    array.splice(index, 1)
                    that.setData({
                        list: array
                    })
                }
            }
        })
    },
    //选择照片方式
    chooseStyle() {
        var that = this;
        var list = that.data.list;
        wx.showActionSheet({
            itemList: ['相册', '拍照', '从QQ会话选择'],
            success(res) {
                if (res.tapIndex == 0) {
                    that.getPhoto('album');
                }
                if (res.tapIndex == 1) {
                    that.getPhoto('camera');
                }
                if (res.tapIndex == 2) {
                    that.getPhoto1();
                }
            },
            fail(res) {

            }
        })
    },
    //拍照调取相册
    getPhoto(type) {
        var that = this;
        wx.chooseImage({
            count: 9,
            sizeType: ['original'],
            sourceType: [type],
            success(res) {
                that.setData({
                    chooseImageArray: res.tempFilePaths
                })
                //轮巡判断上传
                that.uploadFileImg(that.data.chooseImageArray[0], 0);
            }
        })
    },
    getPhoto1() {
        var that = this;
        wx.chooseMessageFile({
            count: 9,
            type: 'image',
            success(res) {
                var array = [];
                for (var i = 0; i < res.tempFiles.length; i++) {
                    array.push(res.tempFiles[i].path)
                }
                that.setData({
                    chooseImageArray: array
                })
                //轮巡判断上传
                that.uploadFileImg(that.data.chooseImageArray[0], 0);
            }
        })
    },
    //上传图片
    uploadFileImg(url, index) {
        var that = this;
        var list = that.data.list;
        var imgs = that.data.chooseImageArray;
        var indexs = parseInt(index) + 1;
        var str = '上传 ' + indexs + '/' + that.data.chooseImageArray.length + '张';
         that.selectComponent("#loading").showLoadingCus(str);
        utils.uploadfile(url, url, '', '', '', function (res) {
            var data = JSON.parse(res.data)
            that.selectComponent("#loading").hideLoadingCus();

            if (data.code == 200) {
                var imgurl = data.data.printUrl;
                var obj = {};
                if (data.data.width > data.data.height) {
                    // obj.fileUrl = imgurl + '?x-oss-process=image/resize,w_742,m_lfit/rotate,90/quality,Q_100/auto-orient,0';
                    obj.fileUrl = imgurl + '?x-oss-process=image/rotate,90/quality,Q_100/auto-orient,0/format,jpg';
                    obj.printUrl = imgurl + '?x-oss-process=image/rotate,90/quality,Q_100/auto-orient,0/format,jpg';
                    // obj.printUrl = imgurl + '?x-oss-process=image/resize,w_742,m_lfit/rotate,90/quality,Q_100/auto-orient,0';
                } else {
                    obj.fileUrl = imgurl + '?x-oss-process=image/quality,Q_100/auto-orient,0/format,jpg';
                    obj.printUrl = imgurl + '?x-oss-process=image/quality,Q_100/auto-orient,0/format,jpg';
                    // obj.fileUrl = imgurl + '?x-oss-process=image/resize,m_lfit,h_742,limit_0/quality,Q_100/auto-orient,0';
                    // obj.printUrl = imgurl + '?x-oss-process=image/resize,m_lfit,h_742,limit_0/quality,Q_100/auto-orient,0';
                }
                // obj.fileUrl = imgurl;
                // obj.printUrl = imgurl;
                obj.taskOrderNo = '';
                obj.machineQrCode = wx.getStorageSync('machineQrCode');
                obj.serviceMainId = that.data.serviceMainId;
                obj.serviceSubtypeId = that.data.serviceMainId;
                obj.fileName = '图片';
                obj.num = 1;
                obj.pageRange = '1-1'
                obj.singleDouble = '1';
                if(that.data.color.indexOf(data.data.color)!=-1){
                    obj.color = data.data.color;
                }else{
                    var arr = that.data.color.split(',')
                    obj.color = arr[0]
                }
                obj.paper = 'A4';
                 obj.tagFloor = that.data.code;
                obj.margins = 1;
                obj.couponListId = '';
                list.push(obj);
                that.setData({
                    list: list
                })
                if (index == imgs.length - 1) {
                    that.setData({
                        chooseImageArray: []
                    })
                    that.selectComponent("#loading").hideLoadingCus();
                } else {
                    that.uploadFileImg(imgs[index + 1], index + 1)
                }
            } else {
                utils.toast(data.msg);
                that.setData({
                    list: [],
                    chooseImageArray: []
                })
            }
        })

    },
            //刚进来获取机器配置
        getConfig(){
            var that = this;
            utils.request('config/getMachineConfig/' + wx.getStorageSync('machineQrCode'), {}, function (res) {
                if(res.data.code==200){
                    var color = ''
                    for(var i=0;i<res.data.data.serviceItem.length;i++){
                        if(res.data.data.serviceItem[i].config.tag=='image'){
                            color = res.data.data.serviceItem[i].config.color
                        }
                    }
                     that.setData({
                        margins:res.data.data.machine.margins,
                        color:color
                    })
                }else{
                    that.setData({
                        margins:1,
                        color:'1,2'
                    })
                }
            })
        },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
         that.getConfig();
        that.setData({
            code: options.code,
            serviceMainId:options.serviceMainId,
            serviceSubtypeId:options.serviceSubtypeId
        })
        wx.getSystemInfo({
            success: (result) => {
                var name = 'iPhone X'
                if (result.model.indexOf(name) > -1) {
                    that.setData({
                        pdb: 68
                    })
                } else {
                    that.setData({
                        pdb: 0
                    })
                }
            },
        })
    },
    submitPhoto() {
        var that = this;
        if (that.data.list.length == 0) {
            utils.alert('至少要上传一张图片');
            return false;
        }
        var that = this;
         that.selectComponent("#loading").showLoadingCus('添加中...');
        var data = that.data.list
        utils.request('printOrderTask/createTasks', data, function (res) {
            if (res.data.code == 200) {
                that.selectComponent("#loading").hideLoadingCus();
                wx.redirectTo({
                    url: '../list/list',
                })

            } else {
                that.selectComponent("#loading").hideLoadingCus();
                utils.toast(res.data.msg)
            }
        })

    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    }
})